package com.gleasy.ibatis;

import ibatis.GenerateBeansAndXmls;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class IbatisUtil {

	private JFrame frmIbatisutil;
	private JTextField url;
	private JTextField packageName;
	private JTextArea log;
	private JPanel panel;
	private JScrollPane scrollPane;
	private JPanel panel_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IbatisUtil window = new IbatisUtil();
					window.frmIbatisutil.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public IbatisUtil() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmIbatisutil = new JFrame();
		frmIbatisutil.setTitle("IbatisUtil");
		frmIbatisutil.setBounds(100, 100, 532, 358);
		frmIbatisutil.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmIbatisutil.getContentPane().setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		frmIbatisutil.getContentPane().add(panel, BorderLayout.NORTH);
				panel.setLayout(new GridLayout(3, 1, 0, 0));
						
								url = new JTextField();
								url.setAlignmentY(0.0f);
								url.setAlignmentX(Component.LEFT_ALIGNMENT);
								panel.add(url);
								url.setText("jdbc:mysql://192.168.0.11:3307/attendance-local-0?user=mysql3307&password=mysql3307");
								url.setColumns(10);
				
						packageName = new JTextField();
						packageName.setAlignmentY(Component.BOTTOM_ALIGNMENT);
						packageName.setAlignmentX(0.0f);
						panel.add(packageName);
						packageName.setText("com.gleasy.attendance.domain");
						packageName.setColumns(10);
						
								JButton go = new JButton("GO");
								panel.add(go);
								go.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent e) {
										log.setText("ING...!");
										new Thread() {
											public void run() {
												try {
													GenerateBeansAndXmls g = new GenerateBeansAndXmls(url.getText(), packageName.getText());
													g.generate();
													log.setText("SUCCESS!");
												} catch (Exception ex) {
													log.setText(ex.getMessage());
												}
											}
										}.start();

									}
								});
						
						scrollPane = new JScrollPane();
						frmIbatisutil.getContentPane().add(scrollPane, BorderLayout.CENTER);
						
						panel_2 = new JPanel();
						scrollPane.setViewportView(panel_2);
						panel_2.setLayout(new BorderLayout(0, 0));
						
								log = new JTextArea();
								log.setEditable(false);
								panel_2.add(log, BorderLayout.CENTER);
								log.setText("1、根据指定的Url，读取数据库表信息，生成Bean文件和Ibatis配置文件到指定的包。\r\n2、生成的文件内容可用Eclipse格式化。\r\n3、映射规则如下：\r\n\tvarchar\t-> String\r\n\tbigint\t-> Long\r\n\tsmallint\t-> Integer\r\n\ttinyint\t-> Byte\r\n\tdatetime\t-> Date\r\n\tdate\t-> java.sql.Date\r\n\ttime\t-> java.sql.Time\r\n\tother\t-> NULL\r\n4、如果你有更好的建议，请联系Rudy。");
	}
}
